<template>
  <div>
    <el-dialog title="单位分配" :visible.sync="dialogVisible" width="400px" :append-to-body="true" @close="closeEditDialog">
      <el-form :model="editRowInfo" :rules="rulesUpdateForm" ref="refUpdateForm" label-width="80px" class="update-form">

        <el-tree :data="dataList" show-checkbox node-key="id" ref="tree" :default-checked-keys="checkedkeys"
          :props="defaultProps">
        </el-tree>

      </el-form>
      <div slot="footer">
        <el-button size="small" @click="closeEditDialog" class="cancel-button">取 消</el-button>
        <el-button size="small" @click="editSubmit" class="confirm-button">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { userAssign } from "@/api/api";
import { mapState } from "vuex";

export default {
  name: "editUser",
  props: ["editRowInfo", "dataList", "checkedkeys"],
  data() {
    return {
      dialogVisible: false,
      // 表单修改验证信息
      rulesUpdateForm: {},
      defaultProps: {
        children: "children",
        label: "orgName",
      },
    };
  },
  computed: {
    ...mapState(["user"]),
  },
  methods: {
    // 修改确认
    editSubmit() {
      // console.log(this.$refs.tree.getCheckedKeys());
      this.$refs.refUpdateForm.validate(async (valid) => {
        if (!valid) return;
        if(this.$refs.tree.getCheckedKeys().length==0) return this.$modal.notifyWarning('必须选择一个单位');
        const res = await userAssign({
          userId: this.editRowInfo.id,
          orgIdList: this.$refs.tree.getCheckedKeys(),
        });
        if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
        this.$modal.notifySuccess("单位分配成功");
        this.closeEditDialog();
        this.$parent.userList(); //更新用户列表
      });
    },
    //点击取消和关闭按钮
    closeEditDialog() {
      this.$refs.tree.setCheckedKeys([]);
      // this.$emit("clearEditInfo"); //清空editInfo,消除视觉残留
      this.$refs.refUpdateForm.resetFields();
      this.dialogVisible = false;
    },
  },
  watch: {
    roleId(val) {
      this.roleIdArr = val;
    },
  },
};
</script>

<style lang="less" scoped>
@import url("../../common.less");
.el-tree {
  background: #283046;
}
// 树形控件字体颜色
::v-deep .el-tree-node__content {
  color: #d0d2d6 !important;
}
// 树形控件选中一行的字体颜色
::v-deep .el-tree-node.is-current > .el-tree-node__content {
  color: #000 !important ;
}
// 节点hover后的背景样式
::v-deep .el-tree-node__content {
  &:hover {
    // background: red !important;
    color: #000 !important;
  }
}
</style>